package com.lagou.edu.service;

import com.lagou.edu.repository.AuthCodeRepository;
import com.lagou.edu.domain.AuthCode;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

/**
 * @author shixiaopeng  on 2021/3/5
 */
@Service
public class CodeServiceImpl implements CodeService {

    @Autowired
    private AuthCodeRepository authCodeRepository;

    //0正确1错误2超时
    @Override
    public int validate(String email, String code) {
        AuthCode authCode = authCodeRepository.findByEmailAndCode(email,code);
        if(authCode==null){
            return 1;
        }
        Date expiretime = authCode.getExpiretime();
        if ((System.currentTimeMillis() - expiretime.getTime()) >= 0) {//过期
            return 2;
        }
        return 0;
    }
}
